﻿using OD.ProjectCustomizationTool.Helpers;
using OD.ToolBox.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace OD.ProjectCustomizationTool
{
    internal abstract class ProjectUpdaterFromQualityFileBase : ProjectUpdaterBase
    {
        #region Properties

        /// <summary>
        /// The name of the quality deposit folder.
        /// </summary>
        protected static String DepositFolderName { get { return Properties.Resources.DepositFolderName; } }

        /// <summary>
        /// Represents the full path to the quality file.
        /// </summary>
        public String QualityFileFullPath
        {
            get
            {
                return Path.Combine(this.DepositFullPath, this.QualityFileName);
            }
        }

        /// <summary>
        /// Represents the file name of the quality file.
        /// </summary>
        protected abstract String QualityFileName { get; }

        /// <summary>
        /// Builds the relative path from the project path to the quality file path.
        /// </summary>
        protected String RelativePathToQualityFile
        {
            get
            {
                return Path.Combine("..", Path.Combine(ProjectUpdaterFromQualityFileBase.DepositFolderName, this.QualityFileName));
            }
        }

        /// <summary>
        /// Retrieve the full path to the original quality file present in the project resources folder.
        /// </summary>
        protected String ResourceQualityFileFullPath { get { return PathHelper.BuildFullPathToResource(this.QualityFileName); } }

        /// <summary>
        /// The full path to the quality deposit folder.
        /// </summary>
        protected String DepositFullPath { get; private set; }

        #endregion

        #region Methods

        protected override void InternalInitialize()
        {
            base.InternalInitialize();

            this.DepositFullPath = Path.Combine(this.SolutionRoot.FullName, ProjectUpdaterFromQualityFileBase.DepositFolderName);
        }

        /// <summary>
        /// Retrieves or builds the quality file.
        /// </summary>
        protected abstract void InternalCreateQualityFile();

        /// <summary>
        /// If the quality file does not already exist, retrieves or builds it in the quality deposit folder
        /// and adds it to the source control.
        /// </summary>
        protected sealed override void BeforeUpdate()
        {
            base.BeforeUpdate();

            if (!File.Exists(this.QualityFileFullPath))
            {
                if (!Directory.Exists(this.DepositFullPath))
                {   // Create the deposit folder if it does not already exist.
                    this.Monitor.Logger.WriteLine("Creating folder " + this.DepositFullPath);
                    Directory.CreateDirectory(this.DepositFullPath);
                }

                this.InternalCreateQualityFile();

                SourceControlHelper.Add(this.QualityFileFullPath, this.Monitor.Logger);
            }
        }

        /// <summary>
        /// <see cref="System.IO.File.Copy(string, string)"/>
        /// </summary>
        protected void CopyFile(String source, String target)
        {
            this.Monitor.Logger.WriteLine("Copying '" + source + "' to '" + target + "'");
            File.Copy(source, target);
        }

        #endregion
    }
}
